#ifndef CAN_H
#define CAN_H
/*********************************************************************
* File Name     : CANLib.h 
* Description   : Header file for CANLib.c                      

* Revision      : 1.0 
* Date          : 02/09/05 
*********************************************************************/

#include <p30F4011.h>
#include "types.h"

#define TRANSMIT_BUFFERS_FULL 0xff
#define NODE_OFFSET 16 // 
#define NODE_0 1
#define NODE_1 2
#define NODE_2 3
#define NODE_3 4
#define NODE_4 5
#define NODE_5 6
#define NODE_6 7
#define NODE_7 8
#define NODE_8 9
#define NODE_9 10
#define NODE_10 11
#define NODE_MASTER 12
#define NODE_CHARGER 32
#define NODE_CONTROLLER 64 

#define NODE NODE_CONTROLLER //  I think I'll have to recompile this stupid thing 6 different times for each node!

#define FILTER_MASK_ACCEPT_ALL  0b00000000000  // Mask everything.
#define FILTER_MASK_ACCEPT_NONE 0b11111111111  // Mask nothing.

/*--- CAN message structure. ---*/

typedef struct {
	uint32_t id; //  could be either 11 or 29 bit ID
	uint8_t length;
	uint8_t data[8];
} CANMSG;
    

/*--- CANLib function prototypes. ---*/

void CANInit(void);                                 /* Call this function first */
void CANSetMask(uint8_t mask_no, uint16_t mask);    /* Then set up masks */ 
void CANSetFilter(uint8_t filter_no, uint16_t sid); /* and filters */
void CANConfig(void);                               /* Then call this function last, to enable can. */

void CANSetPriority(uint8_t buffer, uint8_t priority);
//int CANSendMessage(const CANMSG *message);
int CANSendMessage(CANMSG* message);
int CANReceiveMessage(CANMSG* message);    /* Returns True if message received. */ 
uint8_t GetTXBuffer(void);
void canbus_send_status();
void canbus_receive();
void InitCanbus();

#endif
